package com.itestor.iptv.modules.alert.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.itestor.iptv.global.utils.LoggerUtils;
import com.itestor.iptv.modules.alert.service.RobotLogService;
import com.itestor.iptv.modules.alert.utils.AlertConstant;
import com.itestor.iptv.modules.alert.vo.RobotLogOBJ;
import com.itestor.iptv.modules.common.service.RegionManageService;
import com.itestor.iptv.modules.common.utils.CommonConstant;
import com.itestor.iptv.modules.common.vo.RegionManageOBJ;

/**
 * 
 * @author Mr.li
 * 
 */
public class RobotLogNaviAction extends DispatchAction {

    /**
     * Logger for this class.
     */
    private static final Logger logger = Logger
            .getLogger(RobotLogNaviAction.class);

    private RobotLogService robotLogService;

    /**
     * 确认告警
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward confirm(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String[] pvids = request.getParameterValues("id");
        if (null == pvids) {
            String pvid = request.getParameter("id");
            if (!StringUtils.isEmpty(pvid)) {
                pvids = new String[1];
                pvids[0] = pvid;
            }
        }
        if (null != pvids && pvids.length > 0) {

            long[] ids = new long[pvids.length];
            for (int i = 0; i < pvids.length; i++) {
                try {
                    ids[i] = Long.parseLong(pvids[i]);
                } catch (NumberFormatException e) {
                    ids[i] = 0;
                }
            }

            LoggerUtils.logActionInfo("Confrim the Roblog - id: "
                    + StringUtils.join(pvids, ",") + ".", logger, request);

            robotLogService.confirm(ids);
        }
        
        return mapping.findForward(AlertConstant.FORWARD_ROBOTLOG_CONFIRM);
    }

    /**
     * 删除告警
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward delete(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        String[] pvids = request.getParameterValues("id");
        if (null == pvids) {
            String pvid = request.getParameter("id");
            if (!StringUtils.isEmpty(pvid)) {
                pvids = new String[1];
                pvids[0] = pvid;
            }
        }
        if (null != pvids && pvids.length > 0) {

            long[] ids = new long[pvids.length];
            for (int i = 0; i < pvids.length; i++) {
                try {
                    ids[i] = Long.parseLong(pvids[i]);
                } catch (NumberFormatException e) {
                    ids[i] = 0;
                }
            }

            LoggerUtils.logActionInfo("Delete the user - id: "
                    + StringUtils.join(pvids, ",") + ".", logger, request);

            robotLogService.delete(ids);
        }

        return mapping.findForward(AlertConstant.FORWARD_ROBOTLOG_DELETE);
    }

    /**
     * 
     * @param pRobotLogService
     */
    public void setRobotLogService(RobotLogService pRobotLogService) {
        robotLogService = pRobotLogService;
    }

}
